const express = require('express')
const fs = require("fs")
const path = require("path")

const app = express()

app.listen(3000, () => {
    console.log('服务已开启。。。')
})

//声明中间件函数
const recordMiddleware = (req,res,next) => {
    const { url, ip } = req
    fs.appendFileSync(path.resolve(__dirname, 'access.log'), `${new Date()} ${url} ${ip}\r\n`)
    next()
}
//使用全局中间件
app.use(recordMiddleware)

app.get('/home', (req, res) => {
    res.send('前台首页')
})

app.get('/admin', (req,res) => {
    res.send('后台首页')
})

app.all('*',(req,res) => {
    res.status(404)
    res.send('404 Not Found')
})